<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 * @author   howe <howe1995@163.com>
 */

namespace App\Middleware;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
 * WebSocket token 授权验证中间件.
 */
class WebSocketAuthMiddleware implements MiddlewareInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * 授权验证守卫.
     *
     * @var string
     */
    private $guard = 'jwt';

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // 授权验证拦截握手请求并实现权限检查
        var_dump('授权验证拦截握手请求并实现权限检查:');
        if (auth($this->guard)->guest()) {
            var_dump('授权验证拦截握手请求并实现权限检查:不通过');
            return $this->container->get(\Hyperf\HttpServer\Contract\ResponseInterface::class)->withStatus(403)->raw('Forbidden');
        }
        var_dump('授权验证拦截握手请求并实现权限检查:成功');
        return $handler->handle($request);
    }
}
